package dao;

import bean.AdInfo;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.JDBCUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

/**
 * @ClassName : AdInfoDao
 * @Auther : acerolan@foxmail.com
 * @Data : 2024/4/18 16:11
 * @Description :
 */

public class AdInfoDao {

    static JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Test
    public void test() {
//        System.out.println(GetAdsInfo().get(1));
//        System.out.println(GetAdsInfoById("1"));
//        System.out.println(SearchAdBy2Id("1", "1"));
        System.out.println(GetHotAdsInfo());
    }

    public List<AdInfo> GetHotAdsInfo() {

        try {
            String sql = "SELECT * FROM ad_info ORDER BY ad_hot DESC LIMIT 0, 10";

            return template.query(sql, new BeanPropertyRowMapper<AdInfo>(AdInfo.class));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public AdInfo SearchAdBy2Id(String ad_id, String user_id) {

        try {
            String sql = "SELECT * FROM ad_info WHERE ad_id=? AND user_id=?";

            return template.queryForObject(sql, new BeanPropertyRowMapper<AdInfo>(AdInfo.class), ad_id, user_id);

        } catch (Exception e) {

        }


        return null;
    }

    public int DeleteAdById(String ad_id) {

        try {
            String sql = "DELETE FROM ad_info WHERE ad_id=?";

            return template.update(sql, ad_id);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public int UpdateAdInfo(String ad_brand_name, String ad_website, String ad_company_name, String ad_description, String ad_id) {

        try {
            String sql = "UPDATE ad_info SET ad_brand_name=?, ad_website=?, ad_company_name=?, ad_description=? WHERE ad_id=?";

            return template.update(sql, ad_brand_name, ad_website, ad_company_name, ad_description, ad_id);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public List<AdInfo> GetAdsInfoById(String user_id) {

        try {
            String sql = "SELECT * FROM ad_info WHERE user_id=?";

            return template.query(sql, new BeanPropertyRowMapper<AdInfo>(AdInfo.class), user_id);

        } catch (Exception e) {

        }


        return null;
    }

    public List<AdInfo> GetAdsInfo() {

        try {
            String sql = "SELECT * FROM ad_info where ad_status = 1";

            List<AdInfo> ads = template.query(sql, new BeanPropertyRowMapper<AdInfo>(AdInfo.class));
            System.out.println("数据为："+ads);
            // 广告数量大于10个时，随机抽取nums个
            int nums = 20;
            if (ads.size() > nums) {
                List<AdInfo> tmp = new ArrayList<AdInfo>();

                Random r=new Random();
                HashSet<Integer> set = new HashSet<>();
                while (set.size() < nums) {
                    set.add(r.nextInt(ads.size()));
                }
                for (Integer i: set) {
                    tmp.add(ads.get(i));
                }

                return tmp;
            }

            return ads;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public int BuyAd(String ad_brand_name, String ad_website, String ad_company_name, String ad_description, String ad_status, String ad_size, String ad_img, String user_id, String ad_hot, String ad_date) {

        try {
            String sql = "INSERT INTO ad_info(ad_brand_name, ad_website, ad_company_name, ad_description, ad_status, ad_size, ad_img, user_id, ad_hot, ad_date) VALUE (?,?,?,?,?,?,?,?,?,?)";

            return template.update(sql, ad_brand_name, ad_website, ad_company_name, ad_description, ad_status, ad_size, ad_img, user_id, ad_hot, ad_date);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public int insertAds(AdInfo adInfo) {
        String sql = "INSERT INTO ad_info (ad_brand_name, ad_website, ad_company_name, ad_description, ad_status, ad_size, ad_img, user_id) " +
                "values (?, ?, ?, ?, ?, ?, ?, ?)";

        int update = template.update(sql,
                adInfo.getAd_brand_name(),
                adInfo.getAd_website(),
                adInfo.getAd_company_name(),
                adInfo.getAd_description(),
                adInfo.getAd_status(),
                adInfo.getAd_size(),
                adInfo.getAd_img(),
                adInfo.getUser_id());
        return update;
    }

    public int updateAds(AdInfo adInfo) {
        String sql = "update ad_info set ad_brand_name = ?, ad_website = ?, ad_company_name = ?, ad_description = ?, ad_status = ?, ad_size = ?,  user_id = ? where ad_id = ?";

        int update = template.update(sql,
                adInfo.getAd_brand_name(),
                adInfo.getAd_website(),
                adInfo.getAd_company_name(),
                adInfo.getAd_description(),
                adInfo.getAd_status(),
                adInfo.getAd_size(),
                adInfo.getUser_id(),
                adInfo.getAd_id());
        return update;
    }
    public int delAds(String adIds) {
        String sql = "delete from ad_info where ad_id = ?";
        int update = template.update(sql, adIds);
        return update;
    }

    public static List<AdInfo> getAllInfo(){
        String sql = "SELECT * FROM ad_info";

        List<AdInfo> ads = template.query(sql, new BeanPropertyRowMapper<AdInfo>(AdInfo.class));

        return ads;
    }

    public int updateImg(String adImg,AdInfo adInfo) {
        String sql = "update ad_info set ad_img = ? where ad_id = ?";
        int update = template.update(sql, adImg,adInfo.getAd_id());
        return update;
    }

}
